package hotel.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import jxl.Workbook;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import hotel.service.IHotelOrderService;
import common.action.BaseAction;
import common.pojo.HotelOrder;
import common.utils.FileUtil;


@Scope("prototype")
@Controller("hotelCountAction")
public class HotelCountAction extends BaseAction {

	/**
	 * 统计
	 */
	public String getHotRoomListByPage() throws Exception {
		String type=request.getParameter("type");
		//获取统计列表
		resultlist=getRoomList("");		
		return type==null?"getHotRoomList":"getHotRoomTable";
	}
	
	//搜索
	public String search() throws Exception {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String type=request.getParameter("type");
		String where = "   verify=1 and tradeStatus not in(1,5) and orderTime between '" + startDate + " 00:00:00' and '"+endDate+" 23:59:59'";	
		//获取统计列表
		resultlist=orderService.findSearchList(where);	
		return type==null?"getHotRoomList":"getHotRoomTable";
	}
	

	/**
	 * 获取统计列表
	 */
	private List<HotelOrder> getRoomList(String ids) {
		if(ids!=null&&ids!="")
			return orderService.findHotList(" where verify=1 and tradeStatus not in(1,5) "," where roomstyleId in(" + ids + ")","");
		else
			return orderService.findHotList(" where verify=1 and tradeStatus not in(1,5) ","","");
	}
	

	/**
	 * 导出
	 */
	public void exportData() throws Exception {
		String ids = request.getParameter("ids");
		try {
			// 导出
			response.getWriter().write(export(getRoomList(ids)));
		} catch (Exception e) {
			response.getWriter().write("{\"success\":false,\"msg\":2}");
		}

	}
	private String DOWNLOAD = "download";
	private String export(List<HotelOrder> list)
			throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		String ymd = sdf.format(new Date());
		String savePath = DOWNLOAD + "/" + ymd + "/";
		String newfilename = FileUtil.randomRename(".xls");
		if (!(savePath == null && newfilename == null)) {
			File dirFile = new File(rootPath+savePath);
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(
					rootPath+savePath+newfilename));
			jxl.write.WritableSheet ws = wwb.createSheet("统计", 0);
			ws.addCell(new jxl.write.Label(0, 0, "房间名字"));
			ws.addCell(new jxl.write.Label(1, 0, "总订单数"));
			ws.addCell(new jxl.write.Label(2, 0, "成交订单数"));
			ws.addCell(new jxl.write.Label(3, 0, "取消订单数"));
			ws.addCell(new jxl.write.Label(4, 0, "成交金额"));

			for (int i = 0; i < list.size(); i++) {

				ws.addCell(new jxl.write.Label(0, i + 1, list.get(i)
						.getRoomName()));
				ws.addCell(new jxl.write.Number(1, i + 1, list.get(i)
						.getAll()));
				ws.addCell(new jxl.write.Number(2, i + 1, list.get(i)
						.getOrdernum()));
				ws.addCell(new jxl.write.Number(3, i + 1, list.get(i)
						.getCancel()));
				ws.addCell(new jxl.write.Number(4, i + 1, list.get(i)
						.getTotalMoney()));
			}
			wwb.write();
			wwb.close();
			return "{\"success\":true,\"path\":\""+savePath+newfilename+"\"}";
		} else {
			return "{\"success\":false,\"msg\":1}";
		}
	}

	private static final long serialVersionUID = 1L;
	@Resource
	private IHotelOrderService orderService;
	private List<HotelOrder> resultlist;

	
	public List<HotelOrder> getResultlist() {
		return resultlist;
	}

	public void setResultlist(List<HotelOrder> resultlist) {
		this.resultlist = resultlist;
	}

}
